package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxCdKeyModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.WwdxVipProductModel;
import com.wwdx.service.WwdxCdKeyServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2017-11-02 17:18
 */
@Service
public class WwdxCdKeyServiceImpl extends BaseServiceImpl<WwdxCdKeyModel> implements WwdxCdKeyServiceI {

    @Override
    public Long selectModelCount(WwdxCdKeyModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String sql = "select count(1) from WwdxCdKeyModel\t"+getParams(model,params);
        return count(sql,params);
    }

    private String getParams(WwdxCdKeyModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        String cdkey = model.getCdkey();
        if (StringUtils.isNotBlank(cdkey)){
            sb.append("and cdkey=:cdkey\n");
            params.put("cdkey",cdkey);
        }
        Integer status = model.getStatus();
        if (status != null){
            sb.append("and status =:status\n");
            params.put("status",status);
        }
        String merchantId = model.getMerchant_id();
        if (StringUtils.isNotBlank(merchantId)){
            sb.append("and merchant_id =:merchant_id\n");
            params.put("merchant_id",merchantId);
        }
        return sb.toString();
    }

    @Override
    public List<WwdxCdKeyModel> selectModelList(WwdxCdKeyModel model, Integer page, Integer rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "from WwdxCdKeyModel\t"+getParams(model,params) +"\torder by exchange_time desc";
        List<WwdxCdKeyModel> wwdxCdKeyModels = find(hql, params,page,rows);
        wwdxCdKeyModels.forEach(wwdxCdKeyModel ->{
            Long productId = wwdxCdKeyModel.getProduct_id();
            WwdxVipProductModel wwdxVipProductModel = wwdxVipProductService.getByProductId(productId);
            WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(productId);
            WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(productId);
            if (wwdxVipProductModel != null){
                wwdxCdKeyModel.setProductName(wwdxVipProductModel.getProduct_name());
            } else if(wwdxThemeModel != null){
                wwdxCdKeyModel.setProductName(wwdxThemeModel.getTitle());
            } else if (wwdxBookModel != null){
                wwdxCdKeyModel.setProductName(wwdxBookModel.getBookname());
            }
            String merchantId = wwdxCdKeyModel.getMerchant_id();
            if (StringUtils.isNotBlank(merchantId)){
                wwdxCdKeyModel.setMerchantName(wwdxMerchantService.getById(merchantId).getMerchant_name());
            } else {
                wwdxCdKeyModel.setMerchantName("蜜读");
            }
            String unionid = wwdxCdKeyModel.getUnionid();
            if (StringUtils.isNotBlank(unionid)){
                wwdxCdKeyModel.setUserName(wwdxPersonalService.getByUnionid(unionid).getNickname());
            }
        });
        return wwdxCdKeyModels;
    }

    @Override
    public WwdxCdKeyModel getByCdKey(String cdkey) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("cdkey",cdkey);
        return getByHql("from WwdxCdKeyModel where cdkey=:cdkey",params);
    }

    @Override
    public void addCDKEY(WwdxCdKeyModel model, Integer loop) {
        logger.info("添加兑换码,数量：{}",loop);
        try {
            for (int i = 0; i < loop; i++) {
                model = (WwdxCdKeyModel)model.clone();
                model.setCdkey(super.getStringRandom(6));
                model.setStatus(0);
                model.setGmt_created(new Date());
                this.save(model);
            }
        }catch (CloneNotSupportedException e){
            logger.error("错误信息：{}",e.getMessage());
        }
    }
}